package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.GroupActiveMapper;
import com.bwie.pojo.GroupActive;
import com.bwie.service.GroupActiveService;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupJoin;
import com.bwie.mapper.GroupJoinMapper;
import com.bwie.service.GroupJoinService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 鑫仔
 * @Date: 2025/7/26 11:25
 * @Description:
 **/

@Transactional
@Service
public class GroupJoinServiceImpl extends ServiceImpl<GroupJoinMapper, GroupJoin> implements GroupJoinService {

    @Autowired
    private GroupJoinMapper groupJoinMapper;


    @Autowired
    private HttpServletRequest request;


    @Override
    public R groupJoinList(PageInfoVo pageInfoVo) {
        Page<GroupJoin> groupJoinPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<GroupJoin> page = groupJoinMapper.groupJoinList(groupJoinPage, pageInfoVo);

//        for (GroupJoin record : page.getRecords()) {
//            String joinImages = record.getJoinImages();
//            ArrayList<String> image = new ArrayList<>();
//
//            if (StringUtils.isNotEmpty(joinImages)){
//                List<String> images = JSON.parseArray(joinImages, String.class);
//                record.setImageUrls(images);
//            }
//        }
        List<GroupJoin> records = page.getRecords();
        records.forEach(r -> {
            String json = r.getJoinImages();
            try {
                r.setImageUrls(
                        StringUtils.isBlank(json)
                                ? Collections.emptyList()
                                : JSON.parseArray(json, String.class)
                );
            } catch (Exception e) {
                r.setImageUrls(Collections.emptyList());
            }
        });
        return R.OK(page);
    }


    @Override
    public R addGroupJoin(GroupJoin groupJoin) {


        //活动名称唯一
        GroupJoin groupJoinDb = groupJoinMapper.selectOne(
                new LambdaQueryWrapper<GroupJoin>().eq(GroupJoin::getJoinName, groupJoin.getJoinName())
        );
        if (ObjectUtils.isNotEmpty(groupJoinDb)) {
            return R.ERROR(500, "拼团活动名称不可重复");
        }

        // groupJoin.setJoinImages(JSON.toJSONString(groupJoin.getJoinImages()));
        //审计字段
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);
        groupJoin.setCreateBy(userName);
        groupJoin.setCreateTime(new Date());

        //新增
        groupJoinMapper.insert(groupJoin);
        return R.OK();
    }

    @Override
    public R deleteGroupJoin(Integer joinId) {
        GroupJoin groupJoin = groupJoinMapper.selectById(joinId);
        if (ObjectUtils.isEmpty(groupJoin)) {
            return R.ERROR(500, "不存在");
        }

        groupJoinMapper.deleteById(joinId);
        return R.OK();
    }
}
